package com.csg.ses.controller;

import com.csg.ses.common.annotation.LoginToken;
import com.csg.ses.entity.ses.SysEmployeeEntity;
import com.csg.ses.service.ses.SysEmployeeService;
import com.csg.ses.vo.common.IdListVo;
import com.csg.ses.vo.common.Result;
import com.csg.ses.vo.common.ResultStatus;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import springfox.documentation.annotations.ApiIgnore;

import java.util.List;

/**
 * 人员
 * @author wangjun
 * @date 2020/11/16
 */
@RestController
@RequestMapping("/emp")
@Api(tags = "人员接口")
@ApiIgnore
public class SysEmployeeController {
    @Autowired
    private SysEmployeeService sysEmployeeService;

    /**
     * 列表
     */
    @ApiOperation(value = "人员列表", notes = "")
    @ApiImplicitParams({
        @ApiImplicitParam(name = "emp", value = "人员信息", dataType = "SysEmployeeEntity", paramType = "body"),
        @ApiImplicitParam(name = "current", value = "当前页", required = true ,dataType = "Integer"),
        @ApiImplicitParam(name = "size", value = "每页记录数", required = true ,dataType = "Integer")
    })
    @GetMapping("/list")
    @LoginToken
    public Result<List<SysEmployeeEntity>> list(SysEmployeeEntity emp, Long current, Long size) {
        return sysEmployeeService.retrieve(emp, current, size);
    }

    @GetMapping("/listAll")
    @LoginToken
    public Result<List<SysEmployeeEntity>> list() {
        return sysEmployeeService.queryAllEmployee();
    }

    @ApiOperation(value = "根据id查找人员")
    @ApiImplicitParam(name = "id", value = "人员id", dataType = "String", required = true)
    @GetMapping("/{id}")
    @LoginToken
    public Result<SysEmployeeEntity> getById(@PathVariable String id) {
        return sysEmployeeService.getById(id);
    }

    @ApiOperation(value = "保存")
    @ApiImplicitParam(name = "SysEmployeeEntity", value = "", dataType = "sysEmployee", paramType = "body")
    @PostMapping("/save")
    @LoginToken
    public Result<String> save(@RequestBody SysEmployeeEntity sysEmployee) {
        boolean result = sysEmployeeService.save(sysEmployee);
        System.out.println(result);
        if (result) {
            return new Result("保存成功！", ResultStatus.SUCCESS);
        } else {
            return new Result("保存失败！该用户名已存在", ResultStatus.FAILED);
        }
    }

    /**
     * 删除
     * @param idList
     * @return
     */
    @ApiOperation(value = "删除")
    @ApiImplicitParam(name = "idList", value = "ID数组", dataType = "IdListVo", paramType = "body", required = true)
    @DeleteMapping("/del")
    @LoginToken
    public Result<String> del(@RequestBody IdListVo idList) {
        return sysEmployeeService.del(idList);
    }
}